//
//  CLUpdateView.m
//  MasonryTest
//
//  Created by Charles on 16/9/23.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLUpdateView.h"

@interface CLUpdateView()

@property (nonatomic, strong) UIButton *growingButton;
@property (nonatomic, assign) CGSize buttonSize;

@end

@implementation CLUpdateView

- (instancetype)init{
    if (self = [super init]) {
        [self makeView];
    }
    return self;
}

- (void)makeView{
    self.growingButton.layer.borderColor = [UIColor greenColor].CGColor;
    self.growingButton.layer.borderWidth = 2;
    [self.growingButton addTarget:self action:@selector(didTapGrowButton:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonSize = CGSizeMake(100, 100);
}

- (UIButton *)growingButton{
    if (!_growingButton) {
        _growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_growingButton setTitle:@"Grow Me!" forState:UIControlStateNormal];
        [self addSubview:_growingButton];
    }
    return _growingButton;
}

+ (BOOL)requiresConstraintBasedLayout{
    return YES;
}

- (void)updateConstraints{
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
        make.width.lessThanOrEqualTo(self);
        make.height.lessThanOrEqualTo(self);
    }];
    [super updateConstraints];
}

- (void)didTapGrowButton:(UIButton *)sender{
    self.buttonSize = CGSizeMake(self.buttonSize.width * 1.3, self.buttonSize.height * 1.3);
    // tell constraints they need updating
    [self setNeedsUpdateConstraints];
    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];
    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}

@end
